class Node(object):
    def __init__(self, data=0):
        self.data = data
        self.prev = None
        self.next = None

# 先进先出


class FIFO(object):
    def __init__(self, maxlength):
        self.maxlength = maxlength
        self.length = 0
        self.head = Node()
        self.tail = Node()
        self.tail.prev = self.head
        self.head.next = self.tail

    # 存数据

    def unshift(self, data):

        if self.length == self.maxlength:
            print('IS full,can not add')
        else:
            newNode = Node(data)

            tail = self.tail

            tail.prev.next = newNode
            newNode.prev = tail.prev
            newNode.next = tail
            tail.prev = newNode

            self.length += 1


    # 读取数据

    def shift(self):
        first = self.head.next

        self.length -= 1
        # print(self.length)
        self.head.next = first.next
        first.next.prev = self.head
        return first.data

    # 长度

    def Length(self):
        return self.length

    # 队满
    def isfull(self):
        return self.length == self.maxlength

    # 队空
    def isempty(self):
        return self.length == 0

    def __iter__(self):
        tmpPtr = self.head.next
        while tmpPtr != self.tail:
            yield tmpPtr.data
            tmpPtr = tmpPtr.next


if __name__ == '__main__':
    f = FIFO(8)
    f.unshift(5)
    f.unshift(8)
    f.unshift(90)
    f.unshift(90)
    f.unshift(90)
    f.unshift(90)
    f.unshift(90)
    print(f.shift())
